package com.gwt.dispatcher.dispatcher.shared.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.gwt.dispatcher.dispatcher.shared.Result;

/**
 * This is just the pure ArrayList implementation but setting up the
 * "Result" marker to allow it to be "serializable" with the Dispatcher
 * @author cristian.n.miranda
 *
 * @param <T>
 */
@SuppressWarnings("serial")
public class ArrayListResults<T extends Result> implements Result {

	private ArrayList<T> list;
	
	public ArrayListResults() {}

	public ArrayListResults(List<T> list) {
	    this.list = new ArrayList<T>(list);
	}

	public ArrayListResults(ArrayList<T> list) {
		this.list = list;
	}
	
	public void set(ArrayList<T> list) {
		this.list = list;
	}
	
	public ArrayList<T> get() {
		return this.list;
	}
	
	public static ArrayListResults<StringResult> fromStringCollection(List<String> orig) {
	    
	    ArrayList<StringResult> results = new ArrayList<StringResult>();
	    
	    for (String origStr : orig) {
	        results.add(new StringResult(origStr) );
        }
	 
	    return new ArrayListResults<StringResult>(results);
	}
	
	public static ArrayListResults<StringResult> fromStringCollection(Set<String> orig) {

	    ArrayList<StringResult> results = new ArrayList<StringResult>();

	    for (String origStr : orig) {
	        results.add(new StringResult(origStr) );
	    }

	    return new ArrayListResults<StringResult>(results);
	}
	
	public static List<String> toStringCollection(ArrayListResults<StringResult> in ) {
	    List<String> result = new ArrayList<String>();
	    
	    for (StringResult strResult : in.get()) {
	        result.add(strResult.getValue());
        }

	    return result;
	}
	
}
